﻿Partial Class PrimTahsilat

	Public Sub GenericEventKurum(ByVal e As ProcessEventArgs)
		Select Case e.EventType
			Case EventType.Initialize
				Start("Prim tahsilatı", "İşyeri bilgisini girin.")
				isyerino = AddIsyeri()
				donem = AddDonem("donem", "Dönem", "", DonemeEsasTarih.YM, "Dönemi seçmeniz gerekir")
				bordro = AddCombo("bordro", "Bordro tipi", "A|C|D|D1|D2|E", "width=5", "", "Bordro tipini seçmelisiniz.")
				islemtar = AddDate("islemtar", "İşlem tar.", "nl", "", "İşlem tarihini girmeniz gerekir.")
				StartBody()
				AddList()
				Grid1 = List
				Grid1.AddColumns("durum(;24l),*sno(Sigorta no.;76l),*kimlik(Kimlik no.;80l)")
				Grid1.AddColumns("adi(h),soyadi(h),adisoyadi(Adı soyadı;40%l)")
				Grid1.AddColumns("*pek(PEK;80m),*pogs(PÖGS;42n),pasaport(Pasaport;90l),acik(Açıklama;60%l)")
				Grid1.Sortable = False
				Grid1.AddButton("correct-", "Hatalı kayıtları &düzelt", My.Resources.check)
				Grid1.AddButton("ok", "kayıt", Nothing)
				Grid1.AddButton("error", "hata", My.Resources.ierror)
				Grid1.AddButton("warning", "uyarı", My.Resources.warning)
				Grid1.AddButton("info", "bilgi", My.Resources.Info)
				Grid1.AddButton("-del", "Satırı sil", Nothing)
				Grid1.LeftPanel.Width = 320
				PrepareSummaryPanel()
			Case EventType.Shown
				Emanet = Nothing
				islemtar.set(Remote.SysDate)
				Grid1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
				Grid1.DefaultCellStyle.Padding = New Padding(3, 2, 3, 2)
				Grid1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
				Grid1.ShowLineNumbers = True
				Grid1.Buttons("info").Visible = False
				Grid1.Buttons("warning").Visible = False
				Grid1.Buttons("error").Visible = False
				Grid1.Buttons("ok").Visible = False
				Grid1.Buttons("correct").Visible = False
				UpdateSummary()
			Case EventType.ValidateForm
				If Check("islemtar", islemtar.get < Remote.SysDate, "İşlem tarihi bugünden küçük olamaz.") Then Return
				If Check("islemtar", CDate(islemtar.get).YM > Remote.SysDate.YM, "İşlem tarihi bu ay içinde olmalıdır.") Then Return
				bordrotar = New Date(donem.yil, donem.ay, 1)
				Rsi = Isyeri.IsyeriNoAra(Remote, isyerino.get)
				Asgari = Prim.AsgariUcret(Remote, bordrotar)
				MinPrim = Asgari
				MaxPrim = If(Len(bordro.get) = 1, 5, 7) * MinPrim
				IsyeriBilgileriniOku(isyerino.get)
				Oran.Sigortali = Prim.OranSigortali(bordrotar, bordro.get)
				Oran.Isveren = Prim.OranIsveren(bordrotar, bordro.get, Rsi("tehlike"))
				Oran.Toplam = Prim.Oran(bordrotar, bordro.get, Rsi("tehlike"))
				UpdateSummary()
				AddRow()
				Buttons.Show("Next.Reset")
				Help = "Prim yatırım listesini girip Devam butonunu tıklayın."
				Grid1.Buttons("correct").Visible = True
				Grid1.Buttons("correct").Enabled = False
				Grid1.LeftPanel.Visible = True
				e.Result = True
			Case EventType.Save
				If Not Emanet Is Nothing Then
					Emanet.Refresh(Local)
					If Emanet.Kalan < ValidInfo.ToplamTutar Then
						Warning("Emanet kalan miktarı, bordroyu ödemek için yeterli değildir.")
						Return
					End If
				End If
				If Not (From r As DataGridViewRow In Grid1.Rows Where InStr("[/][i][!]", r.Cells("durum").Value) > 0).Any Then
					Warning("Bu bordroda kaydedilecek bilgi yok.")
					Return
				End If
				If (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value = "[x]").Any Then
					If Not Confirm("Bordroda sorunlu kayıtlar vardır. Kaydını onaylıyor musunuz?") Then Return
				End If
				Try
					'Local.BeginTran()
					Dim bordro1 As New db.prim.bordro
					With bordro1
						.yeni(Local)
						.isyerino.set(isyerino.get)
						.bordrotar.set(bordrotar)
						.bordro.set(bordro.get)
						.pekt.set(ValidInfo.ToplamPek)
						.pogst.set(ValidInfo.ToplamPogs)
						.prim.set(Rsi("tehlike") / 2)
						.iscisay.set(ValidInfo.Count)
						.primtahakkuk.set(ValidInfo.PrimTahakkuk)
						.geczam.set(ValidInfo.CezaTutar)
						.faiz.set(0)
						.tahsilatt.set(ValidInfo.ToplamTutar)
						.borc.set(0)
						.sislem.set("E")
						.primgen.set(ValidInfo.ToplamTutar)
						.islemtar.set(islemtar.get)
						.kaykul.set(User.Kaykul)
						.tkcek.set(Len(bordro.get) - 1)
						.tahsilattipi.set("1")
						Dim Hisse As New Prim.Hisse(bordrotar, bordro.get, .pekt.get, .prim.get)
						With .dagilim
							.makbuzno.set(bordro1.makbuzno.get)
							.shisse.set(Hisse.Sigortali.Toplam)
							.isvhisse.set(Hisse.Isveren.Toplam)
							.devhisse.set(Hisse.Devlet.Toplam)
							.iks.set(Hisse.Sigortali.Iskazasi)
							.ikisv.set(Hisse.Isveren.Iskazasi)
							.ikdev.set(Hisse.Devlet.Iskazasi)
							.iszs.set(Hisse.Sigortali.Issizlik)
							.iszisv.set(Hisse.Isveren.Issizlik)
							.iszdev.set(Hisse.Devlet.Issizlik)
							.hass.set(Hisse.Sigortali.Hastalik)
							.hasisv.set(Hisse.Isveren.Hastalik)
							.hasdev.set(Hisse.Devlet.Hastalik)
							.moys.set(Hisse.Sigortali.Yaslilik)
							.moyisv.set(Hisse.Isveren.Yaslilik)
							.moydev.set(Hisse.Devlet.Yaslilik)
							.anas.set(Hisse.Sigortali.Analik)
							.anaisv.set(Hisse.Isveren.Analik)
							.anadev.set(Hisse.Devlet.Analik)
							.islemtar.set(bordro1.islemtar.get)
							.kaykul.set(bordro1.kaykul.get)
							.tkcek.set(bordro1.tkcek.get)
						End With

						For Each Row As DataGridViewRow In Grid1.Rows
							Select Case Row.Cells("durum").Value
								Case "[/]", "[i]"
									Dim yatirim As New db.prim.bordro.yatirim
									.yatirimlar.Add(yatirim)
									With yatirim
										.isyerino.set(isyerino.get)
										.bordrotar.set(bordrotar)
										.bordro.set(bordro.get)
										.kimlik.set(Row.Cells("kimlik").Value)
										.sno.set(Row.Cells("sno").Value)
										.pasaport.set(Row.Cells("pasaport").Value)
										.pek.set(Row.Cells("pek").Value)
										.pogs.set(Row.Cells("pogs").Value)
										.makbuzno.set(bordro1.makbuzno)
										.kaykul.set(bordro1.kaykul.get)
										.islemtar.set(bordro1.islemtar.get)
										.taksit.set("")
									End With
								Case "[!]"
									Dim yatirim As New db.prim.bordro.problemliyatirim
									.problemliyatirimlar.Add(yatirim)
									With yatirim
										.isyerino.set(isyerino.get)
										.bordrotar.set(bordrotar)
										.bordro.set(bordro.get)
										.kimlik.set(Row.Cells("kimlik").Value)
										.sno.set(Row.Cells("sno").Value)
										.adi.set(Row.Cells("adi").Value)
										.soyadi.set(Row.Cells("soyadi").Value)
										.pasaport.set(Row.Cells("pasaport").Value)
										.pek.set(Row.Cells("pek").Value)
										.pogs.set(Row.Cells("pogs").Value)
										.makbuzno.set(bordro1.makbuzno.get)
										.kaykul.set(bordro1.kaykul.get)
										.islemtar.set(bordro1.islemtar.get)
										.taksit.set("")
									End With
							End Select
						Next

						If Not Emanet Is Nothing Then
							.emanet = New db.prim.emanet
							With .emanet
								.isyerino.set(isyerino.get)
								.tahsilno.set(Emanet.EmanetNo)
								.tahsiltar.set(Emanet.Tarih)
								.makbuz.set(bordro1.makbuzno)
								.makbuzmik.set(ValidInfo.ToplamTutar)
								.emanmikt.set(Emanet.Miktar)
								.emanmikk.set(Emanet.Kalan - ValidInfo.ToplamTutar)
								.islemtar.set(Local.SysTime)
								.kaykul.set(bordro1.kaykul.get)
								.emanettar.set(Emanet.Tarih)
								.hno.set("1")
							End With
						End If

						.save(Local)
					End With


					'For Each Row As DataGridViewRow In Grid.Rows
					'	Select Case Row.Cells("durum").Value
					'		Case "[/]", "[i]"
					'			Prim.Bordro.PrimKaydet(Local, isyerino.get, bordrotar, bordro.get,
					'			 Row.Cells("kimlik").Value, Row.Cells("sno").Value, IsNull(Row.Cells("pasaport").Value, ""),
					'			 Row.Cells("pek").Value, Row.Cells("pogs").Value, makbuzno, User.Kaykul, islemtar.get, "")
					'		Case "[!]"
					'			Prim.Bordro.ProblemliPrimKaydet(Local, isyerino.get, bordrotar, bordro.get,
					'			 Row.Cells("kimlik").Value, Row.Cells("sno").Value, IsNull(Row.Cells("pasaport").Value, ""), Row.Cells("adi").Value, Row.Cells("soyadi").Value,
					'			 Row.Cells("pek").Value, Row.Cells("pogs").Value, makbuzno, User.Kaykul, islemtar.get, "")
					'	End Select
					'Next

					'Prim.Bordro.MakbuzKaydetIsyeri(Local, isyerino.get, bordrotar, bordro.get,
					'ValidInfo.ToplamPek, ValidInfo.ToplamPogs, Rsi("tehlike") / 2, ValidInfo.Count, ValidInfo.PrimTahakkuk,
					'ValidInfo.CezaTutar, 0, ValidInfo.ToplamTutar, 0, makbuzno, "E", "1",
					' islemtar.get, User.Kaykul, 0, ValidInfo.ToplamTutar, Len(bordro.get) - 1)
					'If Not Emanet Is Nothing Then
					'	Prim.Bordro.EmanettenDus(Local, isyerino.get, Emanet.EmanetNo, Emanet.Tarih, makbuzno,
					'	 ValidInfo.ToplamTutar, Emanet.Miktar, Emanet.Kalan - ValidInfo.ToplamTutar, Local.SysTime, User.Kaykul)
					'End If
					'Local.CommitTran()
					Prim.MakbuzYaz(Local, CurrentDepartment, bordro1.makbuzno.get)
					Info("Makbuz kaydedildi. Makbuz numarası : " & bordro1.makbuzno.get)
					Dim EmanetTemp = Emanet
					Reset()
					If Not EmanetTemp Is Nothing Then
						Emanet = EmanetTemp
						Emanet.Refresh(Local)
					End If
				Catch ex As Exception
					Rollback(ex.Message)
					Return
				End Try
			Case EventType.ButtonClicked
				Select Case e.Name
					Case "page0", "page1"
						If Tab = e.Name.Right(1) Then Return
						Tab = e.Name.Right(1)
						If Not Browser.Parent Is Nothing Then
							Browser.Parent.Controls.Remove(Browser)
							Browser = Nothing
						End If
						Restart()
					Case "usb"
						If USBDialog Is Nothing Then
							ShowDialog("Open", "usb")
							USBDialog = Modal
						Else
							ShowDialog(USBDialog)
						End If
					Case "ftp"
						Warning("FTP'den yükleme işlemi henüz desteklenmiyor.")
					Case "file"
						If Not FileOpenDialog.ShowDialog = DialogResult.OK Then Return
						Import(FileOpenDialog.FileName)
					Case "folder"
						If Not FolderOpenDialog.ShowDialog = DialogResult.OK Then Return
						ShowDialog("Open", FolderOpenDialog.SelectedPath)
				End Select
			Case EventType.Other
				Select Case e.Name
					Case "List.correct"
						Correct()
					Case "List.del", "List.Delete"
						If Not Confirm("Bu satırı silmek istediğinizden emin misiniz?") Then Return
						DeleteRow()
					Case "List.ok", "List.error", "List.warning", "List.info"
						For Each Row As DataGridViewRow In Grid1.Rows
							If Not Grid1.IsCurrentCellInEditMode Then
								Select Case e.Name
									Case "List.ok" : Row.Visible = True
									Case "List.error" : Row.Visible = Row.Cells("durum").Value = "[x]"
									Case "List.warning" : Row.Visible = Row.Cells("durum").Value = "[!]"
									Case "List.info" : Row.Visible = Row.Cells("durum").Value = "[i]"
								End Select
							End If
						Next
				End Select
		End Select
	End Sub

	Private Sub Grid1_Propegate(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Grid1.Propegate
		e.Handled = True
		Dim Cell = Grid1.CurrentCell
		If Not Grid1.IsCurrentCellDirty Then
			Select Case Cell.OwningColumn.Name
				Case "sno"
					Grid1.CurrentCell = Cell.OwningRow.Cells("kimlik")
				Case "kimlik"
					Grid1.CurrentCell = Cell.OwningRow.Cells("pek")
				Case "pek"
					Grid1.CurrentCell = Cell.OwningRow.Cells("pogs")
				Case "pogs"
					If Cell.RowIndex = Grid1.Rows.Count - 1 Then
						AddRow()
					Else
						Grid1.CurrentCell = Grid1.Rows(Cell.RowIndex + 1).Cells("sno")
					End If
			End Select
		Else
			Select Case Cell.OwningColumn.Name
				Case "sno"
					If ShowResults("sno", Grid1.EditingControl.Text, Cell.OwningRow) Then
						Grid1.CurrentCell = Cell.OwningRow.Cells("pek")
					Else
						Grid1.CurrentCell = Cell.OwningRow.Cells("kimlik")
					End If
				Case "kimlik"
					ShowResults("kimlik", Grid1.EditingControl.Text, Cell.OwningRow)
					Grid1.CurrentCell = Cell.OwningRow.Cells("pek")
				Case "pek"
					Grid1.CurrentCell = Cell.OwningRow.Cells("pogs")
				Case "pogs"
					If Cell.RowIndex = Grid1.Rows.Count - 1 Then
						AddRow()
					Else
						If Grid1.Rows(Cell.RowIndex + 1).Visible Then Grid1.CurrentCell = Grid1.Rows(Cell.RowIndex + 1).Cells("sno")
					End If
			End Select
		End If
	End Sub

	Sub UpdateSummaryKurum()
		If IsBatch Then Return
		Dim Tarih = Remote.SysDate
		If Not Emanet Is Nothing Then Tarih = Emanet.Tarih
		Oran.Ceza = Prim.GecikmeZammiHesabi(bordro.get, bordrotar, Tarih)
		FileInfo.Count = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "").Count
		FileInfo.ToplamPogs = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "" Select CDec(r.Cells("pogs").Value)).Sum
		FileInfo.ToplamPek = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "" Select CDec(r.Cells("pek").Value)).Sum
		FileInfo.SigortaliTutar = Math.Round(Oran.Sigortali * FileInfo.ToplamPek / 100, 2)
		FileInfo.IsverenTutar = Math.Round(Oran.Isveren * FileInfo.ToplamPek / 100, 2)
		FileInfo.PrimTahakkuk = Math.Round(Oran.Toplam * FileInfo.ToplamPek / 100, 2)
		FileInfo.CezaTutar = Math.Round(Oran.Ceza * FileInfo.PrimTahakkuk / 100, 2)
		FileInfo.ToplamTutar = FileInfo.PrimTahakkuk + FileInfo.CezaTutar
		FileInfo.ErrorCount = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value = "[x]").Count
		FileInfo.WarningCount = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value = "[!]").Count
		ValidInfo.Count = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "" And r.Cells("durum").Value <> "[x]").Count
		ValidInfo.ToplamPogs = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "" And r.Cells("durum").Value <> "[x]" Select CDec(r.Cells("pogs").Value)).Sum
		ValidInfo.ToplamPek = (From r As DataGridViewRow In Grid1.Rows Where r.Cells("durum").Value <> "" And r.Cells("durum").Value <> "[x]" Select CDec(r.Cells("pek").Value)).Sum
		ValidInfo.SigortaliTutar = Math.Round(Oran.Sigortali * ValidInfo.ToplamPek / 100, 2)
		ValidInfo.IsverenTutar = Math.Round(Oran.Isveren * ValidInfo.ToplamPek / 100, 2)
		ValidInfo.PrimTahakkuk = Math.Round(Oran.Toplam * ValidInfo.ToplamPek / 100, 2)
		ValidInfo.CezaTutar = Math.Round(Oran.Ceza * ValidInfo.PrimTahakkuk / 100, 2)
		ValidInfo.ToplamTutar = ValidInfo.PrimTahakkuk + ValidInfo.CezaTutar
		Browser.Clear()
		Browser.Q("<body bgcolor=''><table cellpadding=3 cellspacing=1 width='100%' bgcolor='d0d8d0'>")
		Browser.Q("<tr><td width='50%'>Özet bilgiler</td><td width='25%' align=right>Dosya</td><td width='25%' align=right>Geçerli</td></tr>")
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Sigortalı sayısı", Fn(FileInfo.Count), Fn(ValidInfo.Count))
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Toplam PÖGS", Fn(FileInfo.ToplamPogs), Fn(ValidInfo.ToplamPogs))
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Toplam PEK", Fn(FileInfo.ToplamPek), Fn(ValidInfo.ToplamPek))
		Browser.Q("<tr><td width='40%'>Tutarlar</td><td width='30%' align=right>Dosya</td><td width='30%' align=right>Geçerli</td></tr>")
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Sigortalı (% " & Format(Oran.Sigortali, "00.00") & ")", Fm(FileInfo.SigortaliTutar), Fm(ValidInfo.SigortaliTutar))
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "İşveren (% " & Format(Oran.Isveren, "00.00") & ")", Fm(FileInfo.IsverenTutar), Fm(ValidInfo.IsverenTutar))
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Prim tahakkuk (% " & Format(Oran.Toplam, "00.00") & ")", Fm(FileInfo.PrimTahakkuk), Fm(ValidInfo.PrimTahakkuk))
		If Oran.Ceza > 0 Then Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Gecikme", If(Oran.Ceza = 0, "", " (% " & Format(Oran.Ceza, "00.00") & ")"), Fm(FileInfo.CezaTutar), Fm(ValidInfo.CezaTutar))
		Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Ödenecek", Fm(FileInfo.ToplamTutar), Fm(ValidInfo.ToplamTutar))
		If Not Emanet Is Nothing Then
			'If Not bordro.IsEmpty Then R1.Offset(0, 14)
			Browser.Q("<tr><td colspan=3>Emanet bilgileri</td></tr>")
			Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Emanet tarihi", "", "", DMY(Emanet.Tarih))
			Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Emanet no.", "", Emanet.EmanetNo)
			Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Emanet miktarı", "", Fm(Emanet.Miktar))
			Browser.Q("<tr bgcolor='ffffff'><td>@1</td><td align=right>@2</td><td align=right>@3</td></tr>", "Emanet kalan", "", Fm(Emanet.Kalan))
		End If
		Browser.Q("</table></body>")
	End Sub

End Class
